package com.acme.bankapp.domain.bank.account;

import com.acme.bankapp.domain.bank.exception.OverDraftLimitExceededException;

import java.io.Serializable;

/**
 * This class implements {@link AbstractAccount} interface for clients with overdraft
 *
 * @author Alexander Kotov
 */

public class CheckingAccount extends AbstractAccount {

    private final double overdraft;

    public CheckingAccount(double balance, double overdraft) throws IllegalArgumentException {
        super(balance);
        if (overdraft < 0) {
            throw new IllegalArgumentException("Negative overdraft");
        }
        this.overdraft = overdraft;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void withdraw(double amount) throws OverDraftLimitExceededException {
        if (balance + overdraft >= amount) {
            balance -= amount;
        } else {
            throw new OverDraftLimitExceededException(this);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public double getMaximumAmountToWithdraw() {
        return balance + overdraft;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        if (!super.equals(o)) {
            return false;
        }

        CheckingAccount that = (CheckingAccount) o;

        if (Double.compare(that.overdraft, overdraft) != 0) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        long temp = Double.doubleToLongBits(overdraft);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        return result;
    }

    @Override
    public String toString() {
        return super.toString() + "; account type = checking; overdraft = " + overdraft;
    }
}
